package program1;
import java.io.*;


public class filein {//与写入写出相关的代码
	public static void output(String ms) throws IOException
	{
		FileOutputStream fos = new FileOutputStream("./hotel message/message.txt",true);//创建输入流
		String regex = "\\s";
		String []word = ms.split(regex);//切割
		if((check(word[1])==1))//检查是否有与本地中相同的id
		{
			delete(word[1]);//有则删除原来的
		}
		byte[] byte1 = word[1].getBytes();//写入到本地文件中
		byte[] byte2 = word[2].getBytes();
		fos.write("ID: ".getBytes());
		fos.write(byte1);
		fos.write("     用户名: ".getBytes());
		fos.write(byte2);
		fos.write("\n".getBytes());	
		fos.close();
	}
//	
	
	
	public static void input() throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("./hotel message/message.txt")));//创建输出流
		int data;//用于读取数据
		StringBuilder buffer = new StringBuilder();//实现字符的拼接
		String regex = "\\s";//定义正则表达式
		while((data = br.read())!=-1) 
		{
			buffer.append((char)data);	
			String receivedString = buffer.toString();
			if (data=='\n')
			{				
				String []word = receivedString.split(regex);//切割				
				MyHashMay.set(word[1], word[7]);//将本地文件存入哈希表中
				buffer = new StringBuilder();//重置buffer
			}
				
		}
	}
//	
	
	
	
	public static int check(String key) throws IOException//用于本地文件中是否存在相同的id
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("./hotel message/message.txt")));//创建输出流
		int data;//用于读取数据
		StringBuilder buffer = new StringBuilder();//实现字符的拼接
		String regex = "\\s";//定义正则表达式
		while((data = br.read())!=-1) 
		{
			buffer.append((char)data);	
			String receivedString = buffer.toString();
			if (data=='\n')
			{
				String []word = receivedString.split(regex);//切割				
					if(key.equals(word[1]))
						return 1;
				buffer = new StringBuilder();//重置buffer
			}	
		}
		return 0;
	}		
//


	public static void delete(String key) throws IOException//用于删除指定id的信息
	{
		FileOutputStream fos = new FileOutputStream("./hotel message/interim.txt");//创建临时文件的输入流
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("./hotel message/message.txt")));//创建本地文件输出流
		int data;//用于读取数据
		StringBuilder buffer = new StringBuilder();//实现字符的拼接
		String regex = "\\s";//定义正则表达式
		while((data = br.read())!=-1) 
		{
			buffer.append((char)data);	
			String receivedString = buffer.toString();
			if (data=='\n')
			{				
				String []word = receivedString.split(regex);//切割				
				if(key.equals(word[1]))//查找相同的id
				{					
					buffer = new StringBuilder();//重置buffer
					continue;
				}
				else//将不删除的数据写入临时文件	
				{					
					fos.write("ID: ".getBytes());
					fos.write(word[1].getBytes());
					fos.write("     用户名: ".getBytes());
					fos.write(word[7].getBytes());
					fos.write("\n".getBytes());		
					buffer = new StringBuilder();//重置buffer
				}
			}	
		}
		BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream("./hotel message/interim.txt")));//创建临时文件输出流
		FileOutputStream fos1 = new FileOutputStream("./hotel message/message.txt");//重置本地文件
		while((data = br1.read())!=-1) 
		{
			buffer.append((char)data);	
			String receivedString = buffer.toString();
			if (data=='\n')
			{
				String []word = receivedString.split(regex);//切割
				fos1.write("ID: ".getBytes());//将文件重新写入本地
				fos1.write(word[1].getBytes());
				fos1.write("     用户名: ".getBytes());
				fos1.write(word[7].getBytes());
				fos1.write("\n".getBytes());		
				buffer = new StringBuilder();//重置buffer	
			}	
		}
		
	}

}














